package cn.iocoder.an.module.business.service.warningconfig;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.an.module.system.service.user.AdminUserService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.an.module.business.controller.admin.warningconfig.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.warningconfig.WarningConfigDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.warningconfig.WarningConfigMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 预警配置 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class WarningConfigServiceImpl implements WarningConfigService {

    @Resource
    private WarningConfigMapper warningConfigMapper;

    @Resource
    private AdminUserService userService;

    @Override
    public Long createWarningConfig(WarningConfigSaveReqVO createReqVO) {
        // 插入
        setOtherData(createReqVO);
        WarningConfigDO warningConfig = BeanUtils.toBean(createReqVO, WarningConfigDO.class);
        warningConfigMapper.insert(warningConfig);

        // 返回
        return warningConfig.getId();
    }

    @Override
    public void updateWarningConfig(WarningConfigSaveReqVO updateReqVO) {
        // 校验存在
        setOtherData(updateReqVO);
        validateWarningConfigExists(updateReqVO.getId());
        // 更新
        WarningConfigDO updateObj = BeanUtils.toBean(updateReqVO, WarningConfigDO.class);
        warningConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarningConfig(Long id) {
        // 校验存在
        validateWarningConfigExists(id);
        // 删除
        warningConfigMapper.deleteById(id);
    }

    @Override
        public void deleteWarningConfigListByIds(List<Long> ids) {
        // 删除
        warningConfigMapper.deleteByIds(ids);
        }


    private void validateWarningConfigExists(Long id) {
        if (warningConfigMapper.selectById(id) == null) {
            throw exception(WARNING_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public WarningConfigDO getWarningConfig(Long id) {
        return warningConfigMapper.selectById(id);
    }

    @Override
    public PageResult<WarningConfigDO> getWarningConfigPage(WarningConfigPageReqVO pageReqVO) {
        return warningConfigMapper.selectPage(pageReqVO);
    }

    public void setOtherData(WarningConfigSaveReqVO reqVO){
        List<Long> dutyUserId = reqVO.getWarningUser();
        List<AdminUserDO> userList = userService.getUserList(dutyUserId);
        String dutyUserName = userList.stream()
                .map(AdminUserDO::getNickname)                    // 提取 nickname
                .filter(nickname -> nickname != null && !nickname.trim().isEmpty()) // 过滤 null/空
                .collect(Collectors.joining(","));               // 用逗号拼接
        reqVO.setWarningUserName(dutyUserName);
    }

}